<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Questions extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->helper(array('link','answer'));
		$this->load->library(array("form_validation"));
		$this->load->model(array('questions_model', 'apply_model'));
		session_start();
		
	}
	
	
	function index(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			#get data.
			$verify_num				=	$this->input->post('verification_num');
		
			#set rules.
			$this->form_validation->set_rules("info_name","Name","trim|xss_clean");
			$this->form_validation->set_rules("info_email","Email","trim|xss_clean");
			$this->form_validation->set_rules("brief","Brief of question","trim|xss_clean");
			$this->form_validation->set_rules("question_detail","Details of question","trim|xss_clean");
				
			if($this->form_validation->run() == TRUE){
				$info					=	array();
				$info['sender']			=	$this->input->post('info_name');
				$info['sender_email']	=	$this->input->post('info_email');
				$info['brief']			=	$this->input->post('brief');
				$info['brief_ascii']	=	ascii_link($this->input->post('brief'));
				$info['detail']			=	$this->input->post('question_detail');
				$info['time']			=	date('Y-m-d H:i:s', time());
				
				#vertify
				if($verify_num == $this->session->userdata('verify_num_ans')){
					#insert into DB.
					$this->questions_model->submit_question($info);
					$data['inform']			=	"true";
					$data['error']			=	"false";
				}else{
					$data['error']			=	"true";
					$data['inform']			=	"false";
				}
				$this->session->sess_destroy('verify_num_ans');
			}
		}
		
		#get data from DB.
		$questions_list				=	$this->questions_model->get_questions_list();
		$country					=	$this->apply_model->get_country();
		$questions_cate				=	$this->questions_model->get_questions_cate();
		
		#config for pagination.
		$total_items				= 	count($questions_list);
		$perpage					=	5;
		
		$this->load->library('pagination');
		$config['total_rows'] 		= 	$total_items;
		$config['per_page'] 		= 	$perpage;
		$config['first_link']		= 	'First';
		$config['last_link']		= 	'Last';
		$config['next_link']		= 	'>>';
		$config['prev_link']		= 	'<<';
		$config['first_tag_open']	= 	'<span>';
		$config['first_tag_close']	= 	'</span>';
		$config['last_tag_open']	= 	'<span>';
		$config['last_tag_close']	= 	'</span>';
		$config['num_tag_open']		= 	'<span style="padding:0 5px 0 5px">';
		$config['num_tag_close']	= 	'</span>';
		$config['num_links']		= 	4;
		$config['cur_tag_open']		= 	'<span style="padding:0 5px 0 5px;color:#ffffff;background-color:#0373BC;">';
		$config['cur_tag_close']	= 	'</span>';
		$config['base_url']			= 	base_url().'questions-and-answers/';
		$config['uri_segment']		= 	2;
		
		$this->pagination->initialize($config);
		$pagination					= 	$this->pagination->create_links();
		$offset 					= 	($this->uri->segment(2)=='')?0:$this->uri->segment(2);
		
		$questions					=	$this->questions_model->get_paging_list($perpage, $offset);	
		
		#vertify number question.
		$rand_number				=	rand(99996,999999);
		$this->session->set_userdata('verify_num_ans', $rand_number);
		
		#assign data.
		$data['questions_list']		=	$questions;	
		$data['pagination']			=	$pagination;
		$data['country']			=	$country;
		$data['verify']				=	$rand_number;
		$data['questions_cate']		=	$questions_cate;

		$data['tpl_file']			=	"q&a/questions";
		$this->load->view('layout/index', $data);
	}
	
	
	function detail($question_link = ""){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			#set rules.
			$this->form_validation->set_rules("ans_name","Name","trim|xss_clean");
			$this->form_validation->set_rules("ans_email","Email","trim|xss_clean");
			$this->form_validation->set_rules("answer_detail","Details of answer","trim|xss_clean");
			
			if($this->form_validation->run() == TRUE){
				$info					=	array();
				$info['sender']			=	$this->input->post('ans_name');
				$info['sender_email']	=	$this->input->post('ans_email');
				$info['answer']			=	$this->input->post('answer_detail');
				$info['id_question']	=	$this->input->post('question_id_hidd');
				$info['time']			=	date('Y-m-d H:i:s', time());
				$vertify_number			=	$this->input->post('vertication_num_ans');
				
				if($vertify_number == $this->session->userdata('vertify_number')){
					#update answer into DB.
					$this->questions_model->submit_answer($info);
					$data['inform']		=	"true";
					$data['error']		=	"false";
				}else{
					$data['error']		=	"true";
					$data['inform']		=	"false";
				}
				$this->session->unset_userdata('vertify_number');
			}
		}
		#get data from DB.
		$country				=	$this->apply_model->get_country();
		$questions_cate			=	$this->questions_model->get_questions_cate();
	
		#get question matches with $question_link.
		$question_detail		=	$this->questions_model->get_question_detail($question_link);
		$matching_answers		=	$this->questions_model->get_matching_answers($question_detail['id']);
		$other_questions		=	$this->questions_model->get_other_questions($question_detail['id']);
		
		#vertify number answer .
		$rand_number_ans		=	rand(99996,999999);
		$this->session->set_userdata('vertify_number', $rand_number_ans);
	
		#assign data.
		$data['question']		=	$question_detail;
		$data['answers']		=	$matching_answers;
		$data['others']			=	$other_questions;
		$data['country']		=	$country;
		$data['quest_link']		=	$question_link;
		$data['vertify_num']	=	$rand_number_ans;
		$data['questions_cate']	=	$questions_cate;
		
		$data['tpl_file']		=	"q&a/answer";
		$this->load->view('layout/index', $data);
	}
	

	
	function search(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$key				=	$this->input->post('question');
		}else{
			redirect(base_url());
		}
		#get country.
		$country				=	$this->apply_model->get_country();
		
		#get datas match with keyword.
		$keyword				=	$key;
		$questions				=	$this->questions_model->search_results($key);
		
		#assign data.
		$data['results_list']	=	$questions;
		$data['keyword']		=	$keyword;
		$data['country']		=	$country;
			
		$data['tpl_file']		=	"q&a/search";
		$this->load->view('layout/index', $data);
		
	}
	
	
	function category($title_link = ""){
		#get data from DB.
		$country				=	$this->apply_model->get_country();
		$questions_cate			=	$this->questions_model->get_questions_cate();
		$cate					=	$this->questions_model->get_cate($title_link);
		$questions				=	$this->questions_model->get_categories($cate['id']);
		
		#config for pagination.
		$total_items				= 	count($questions);
		$perpage					=	5;
		
		$this->load->library('pagination');
		$config['total_rows'] 		= 	$total_items;
		$config['per_page'] 		= 	$perpage;
		$config['first_link']		= 	'First';
		$config['last_link']		= 	'Last';
		$config['next_link']		= 	'>>';
		$config['prev_link']		= 	'<<';
		$config['first_tag_open']	= 	'<span>';
		$config['first_tag_close']	= 	'</span>';
		$config['last_tag_open']	= 	'<span>';
		$config['last_tag_close']	= 	'</span>';
		$config['num_tag_open']		= 	'<span style="padding:0 5px 0 5px">';
		$config['num_tag_close']	= 	'</span>';
		$config['num_links']		= 	4;
		$config['cur_tag_open']		= 	'<span style="padding:0 5px 0 5px;color:#ffffff;background-color:#0373BC;">';
		$config['cur_tag_close']	= 	'</span>';
		$config['base_url']			= 	base_url().'categories/'.$title_link.'/';
		$config['uri_segment']		= 	3;
		
		$this->pagination->initialize($config);
		$pagination					= 	$this->pagination->create_links();
		$offset 					= 	($this->uri->segment(3)=='')?0:$this->uri->segment(3);
		
		$questions_list				=	$this->questions_model->get_cate_paging($perpage, $offset, $cate['id']);
		
		#assign data.
		$data['questions_list']	=	$questions_list;
		$data['questions_cate']	=	$questions_cate;
		$data['country']		=	$country;
		$data['pagination']		=	$pagination;
		
		$data['tpl_file']	=	"q&a/category";
		$this->load->view('layout/index', $data);
	}


}
/* End of file questions.php */
/* Location: ./application/controllers/questions.php */